﻿using System;
using GeoAPI.Geometries;

namespace NetTopologySuite.Operation.Predicate
{
	public class RectangleIntersects
	{
		public static bool Intersects(IPolygon rectangle, IGeometry b)
		{
			return new RectangleIntersects(rectangle).Intersects(b);
		}

		public RectangleIntersects(IPolygon rectangle)
		{
			this._rectangle = rectangle;
			this._rectEnv = rectangle.EnvelopeInternal;
		}

		public bool Intersects(IGeometry geom)
		{
			if (!this._rectEnv.Intersects(geom.EnvelopeInternal))
			{
				return false;
			}
			EnvelopeIntersectsVisitor envelopeIntersectsVisitor = new EnvelopeIntersectsVisitor(this._rectEnv);
			envelopeIntersectsVisitor.ApplyTo(geom);
			if (envelopeIntersectsVisitor.Intersects)
			{
				return true;
			}
			GeometryContainsPointVisitor geometryContainsPointVisitor = new GeometryContainsPointVisitor(this._rectangle);
			geometryContainsPointVisitor.ApplyTo(geom);
			if (geometryContainsPointVisitor.ContainsPoint)
			{
				return true;
			}
			RectangleIntersectsSegmentVisitor rectangleIntersectsSegmentVisitor = new RectangleIntersectsSegmentVisitor(this._rectangle);
			rectangleIntersectsSegmentVisitor.ApplyTo(geom);
			return rectangleIntersectsSegmentVisitor.Intersects;
		}

		public const int MaximumScanSegmentCount = 200;

		private readonly IPolygon _rectangle;

		private readonly Envelope _rectEnv;
	}
}
